<template>
  <div>
    <div class="app-container">
      <!-- 渲染组件 -->
      <classPageTools @isShowDialog="isShowDialog = true" @search="search" @input="input" />
    </div>
    <el-card class="tools">
      <el-table border :data="classificationList">
        <el-table-column label="图片" width="150px">
          <template #default="{row}">
            <img :src="row.image" alt="" style="width:100%" class="staffPhoto">
          </template>
        </el-table-column>

        <el-table-column label="标题" prop="title" />

        <el-table-column label="介绍" prop="desc" />
        <el-table-column label="操作" fixed="right" width="280">
          <template slot-scope="{row}">

            <el-button type="primary" size="small" @click="bj(row.id)">编辑</el-button>
            <el-button type="danger" size="small" @click="classification(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页组件 -->
      <el-row type="flex" justify="center" align="middle" style="height: 60px">
        <el-pagination
          layout="prev, pager, next"
          :total="total"
          :page-size="page.pageSize"
          :current-page="page.page"
          @current-change="currentChange"
        />
      </el-row>
    </el-card>
    <!-- 弹窗star -->
    <el-dialog :title="`${list.id?'编辑':'新增'}分类`" :visible="isShowDialog" @close="btnCancel">
      <el-form ref="perForm" :model="list" :rules="rules" label-width="100px" class="demo-ruleForm">
        <el-form-item label="标题" prop="title">
          <el-input v-model="list.title" />
        </el-form-item>
        <el-form-item label="描述" prop="desc">
          <el-input v-model="list.desc" type="textarea" :autosize="{ minRows: 4, maxRows: 8}" />
        </el-form-item>
        <el-form-item label="封面">
          <classImageUpload ref="formDataPhoto" @formData="photo" />
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" type="primary" @click="btnOk">确定</el-button>
          <el-button size="small" @click="btnCancel">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
    <!-- 弹窗end -->
  </div>
</template>

<script>
import { getclassificationList, addclass, deleclass, putclass, getclass, getcountclass } from '@/api/Classification'
export default {
  data() {
    return {
      rules: {
        title: [
          { required: true, message: '请输入标题', trigger: 'blur' }

        ],
        desc: [
          { required: true, message: '描述不能为空', trigger: 'blur' }
        ]
      },
      isShowDialog: false,
      classificationList: [], // 列表
      list: {
        title: '',
        desc: '',
        image: []

      }, // 弹窗数据
      formData: {
        _limit: 5, // 一个几条数据
        _start: 0, // 页码  0为第一页
        title_contains: ''
      },
      page: {
        pageSize: 5, // 每页显示条数
        page: 1// 默认第一页
      },
      total: 0
    }
  },
  created() {
    this.getclassificationList()
    this.getcountclass()
    // console.log(99, this.total)
  },
  methods: {
    // 当输入框没有数据的时候还原数据
    input() {
      console.log('没数据了')
      this.formData = {
        _limit: 5, // 一个几条数据
        _start: 0, // 页码  0为第一页
        title_contains: ''
      }
      this.page.pageSize = 5
      this.getclassificationList()
      this.getcountclass()
    },
    // 总数
    async getcountclass() {
      this.total = await getcountclass()
    },
    async getsearch() {
      this.total = await getcountclass(this.formData.title_contains)
    },
    // 搜索  子传父用形参接收
    async search(title) {
      // console.log('搜索', title)
      // 数据赋值
      this.formData.title_contains = title
      // console.log(127, this.formData.title_contains)
      // 显示内容
      await this.getclassificationList()
      // 获取分页总数
      this.total = this.classificationList.length
    },
    // 分页
    currentChange(page) {
      // 把最新的页数传给当前页
      this.page.page = page
      this.formData._start = (page - 1) * this.formData._limit
      this.getclassificationList()
    },
    // 编辑
    async bj(id) {
      // console.log('编辑', id)
      this.list = await getclass(id)
      // 获取DOM
      this.$nextTick(() => {
        // 这里获取到组件DOM后 给她赋值 fileList 要和组件页面的fileList一样
        // 赋值 数组包对象  url:
        this.$refs.formDataPhoto.fileList = [{ url: this.list.image }]
      })

      this.isShowDialog = true
    },
    // 删除
    async classification(id) {
      // console.log('删除', id)
      try {
        await this.$confirm('确定要删除吗？')
        await deleclass(id)
        this.$message.success('删除成功')
        this.getclassificationList()
      } catch (err) {
        console.log(err)
      }
    },
    // 获取数据
    async getclassificationList() {
      this.classificationList = await getclassificationList(this.formData)
    },
    // 确定按钮
    async btnOk() {
      // 校验表单
      await this.$refs.perForm.validate()
      // 判断是否是编辑
      if (this.list.id) {
        console.log(167, this.list.id)
        await putclass(this.list)
        // 提示用户
        this.$message.success('编辑成功')
      } else {
        // 发请求
        await addclass(this.list)
        // 提示用户
        this.$message.success('新增成功')
      }
      // 更新数据
      this.getclassificationList()
      this.getcountclass()
      // 关闭弹窗
      this.isShowDialog = false
    },
    // 取消按钮  新增：点击之后 再次进入要清空
    btnCancel() {
      // console.log('取消')
      // console.log(146, this.list.id)
      try {
        if (this.list.id) {
          this.$nextTick(() => {
          // console.log(151, this.$refs.formDataPhoto)
          })
        }
        // 清除数据
        this.list = {
          title: '',
          desc: '',
          image: []
        }
        // 清除上一次的图片
        this.$refs.formDataPhoto.fileList = []
        // 取消表单校验
        this.$refs.perForm.resetFields()
        // 弹窗关闭
        this.isShowDialog = false
      } catch (err) {
        console.log(err)
      }
    },
    // 接收子组件的事件
    photo(data) {
      this.list.image = data.url
    }
  }
}
</script>

<style lang="scss" scoped>
.tools {
    margin: 10px 20px;
 }
</style>
